package com.jcl.recruit.console.user;

import com.jcl.core.security.service.UserDetailService;
import com.jcl.recruit.console.user.dao.SysUserDao;
import com.jcl.recruit.console.user.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName UserDetailServiceImpl

 * @Author yyliu
 * @Date 2018/12/18 15:07
 * @Version 1.0
 **/
@Component("userDetailService")
public class UserDetailServiceImpl implements UserDetailService {

    @Autowired
    private SysUserDao userDao;

    @Override
    public UserDetails findOne() {
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        SysUser user = userDao.findOne(s);

        Set authoritiesSet = new HashSet();
        // 模拟从数据库中获取用户角色
        GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_ADMIN");

        authoritiesSet.add(authority);
        user.setAuthorities(authoritiesSet);

        return user;
    }
}
